require "public"
local MJLogic = require "common/MJLogic"

local table_insert = table.insert
local table_remove = table.remove

local MJ_Player = class("MJ_Player")

function MJ_Player:ctor(finish)
    self.total_score = 0
	self.is_ready_ = false
	self.is_out_line_ = false	
	self:reset_game(finish)
	self.score_stat = {}
	self.is_double = 1 
	self.is_piao = 0 
	self.latitude = 0 
	self.longitude = 0 
	self.gps_type = 0 
end

function MJ_Player:reset_game(finish)
    self.hand_ = {}
    self.group_ = {}
	self.out_cards_ = {}
	self.hu_types = {}
	self.hu_cards = {}
	self.score_ = 0
	self.fan	= 0
	self.xia_jingfen	= 0
	self.huotou_jingfen = 0
	self.huitou_zhengjing_num = 0
	self.huitou_fujing_num = 0
	self.jingfen = 0
	self.dianpao 			= false
	self.zimo 				= false
	self.is_jiepao 			= false
	self.tiandihu 			= false
	self.minggang 			= 0
	self.angang 			= 0
	self.last_jiesan_time 	= 0
	self.louhu_score		= false
	self.is_out_line_		= false
	self.cant_gang_cards 	= {}
	self.cant_peng_cards 	= {}
	self.last_card 	= nil
	self.is_angang 	= 0
	self._flag 	= 0
end

function MJ_Player:set_flag(tag)
	self._flag = tag
end

function MJ_Player:get_flag()
	return self._flag
end

function MJ_Player:set_gps(latitude,longitude,type)
	self.latitude = latitude
	self.longitude = longitude
	self.gps_type = type
end
function MJ_Player:get_gps()
	return self.latitude,self.longitude,self.gps_type
end
 
local function rad(d) 
    local rs = d * math.pi / 180.0
    return rs
end


 
local function transformLat(x,y) 
    local ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(math.abs(x))
    ret = ret + (20.0 * math.sin(6.0 * x * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0
    ret = ret + (20.0 * math.sin(y * math.pi) + 40.0 * math.sin(y / 3.0 * math.pi)) * 2.0 / 3.0
    ret = ret + (160.0 * math.sin(y / 12.0 * math.pi) + 320 * math.sin(y * math.pi / 30.0)) * 2.0 / 3.0
    return ret
end

local function transformLon(x, y)
    local ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(math.abs(x))
    ret = ret + (20.0 * math.sin(6.0 * x * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0
    ret = ret + (20.0 * math.sin(x * math.pi) + 40.0 * math.sin(x / 3.0 * math.pi)) * 2.0 / 3.0
    ret = ret + (150.0 * math.sin(x / 12.0 * math.pi) + 300.0 * math.sin(x / 30.0 * math.pi)) * 2.0 / 3.0
    return ret
end

local function delta(lat, lon)  
    local a = 6378245.0
    local ee = 0.00669342162296594323
    local dLat = transformLat(lon - 105.0, lat - 35.0)
    local dLon = transformLon(lon - 105.0, lat - 35.0)
    local radLat = lat / 180.0 * math.pi
    local magic = math.sin(radLat)
    magic = 1 - ee * magic * magic
    local sqrtMagic = math.sqrt(magic)
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * math.pi)
    dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * math.pi)
    return dLat,dLon
end

function MJ_Player:WGStoGCJ(wgsLat,wgsLon)
    local dLat,dLon = delta(wgsLat, wgsLon)
    wgsLat = wgsLat + dLat
    wgsLon = wgsLon + dLon
    return wgsLat,wgsLon
end

function MJ_Player:getDistance(lat1,lng1,lat2,lng2)
	local radLat1 = rad(lat1)
	local radLat2 = rad(lat2)
	local a = radLat1 - radLat2
	local b = rad(lng1) - rad(lng2)
	local aa = math.sin(a/2)
	local bb = math.sin(b/2)
	local s = 2 * math.asin(math.sqrt( aa * aa + 
	math.cos(radLat1)*math.cos(radLat2)* bb * bb))
	s = math.floor(s * 6378137.0) -- 单位米 6378137  
	return s
end

function MJ_Player:set_is_angang(tag)
	self.is_angang = tag
end

function MJ_Player:get_is_angang()
	return self.is_angang
end

function MJ_Player:set_last_card(tag)
	self.last_card = tag
end

function MJ_Player:get_last_card() 
    local count = self:get_hand_count()
    if count == 2 or count == 5 or count == 8 or count == 11 or count == 14 then
		return self.last_card
	else
		return nil
	end
end
function MJ_Player:add_cant_gang_card(card)
	table.insert(self.cant_gang_cards,card)
end

function MJ_Player:get_cant_gang_card()
	return self.cant_gang_cards
end

function MJ_Player:add_cant_peng_card(card)
	table.insert(self.cant_peng_cards,card)
end

function MJ_Player:get_cant_peng_card()
	return self.cant_peng_cards
end

function MJ_Player:clean_cant_peng_card()
	self.cant_peng_cards = {}
end

function MJ_Player:has_hand_card_num(card)
	return MJLogic.has_card_num(self.hand_, card)
end

function MJ_Player:set_is_double(tag)
	self.is_double = tag
end

function MJ_Player:get_is_double()
	return self.is_double
end

function MJ_Player:set_is_piao(tag)
	self.is_piao = tag
end

function MJ_Player:get_is_piao()
	return self.is_piao
end

function MJ_Player:set_out_line(flag)
	self.is_out_line_ = flag
end

function MJ_Player:is_out_line()
	return self.is_out_line_
end

function MJ_Player:set_louhu_score(tag)
	self.louhu_score = tag
end

function MJ_Player:get_louhu_score()
	return self.louhu_score
end

function MJ_Player:set_huitou_zhengjing_num(tag)
	self.huitou_zhengjing_num = tag
end

function MJ_Player:get_huitou_zhengjing_num()
	return self.huitou_zhengjing_num
end

function MJ_Player:set_huitou_fujing_num(tag)
	self.huitou_fujing_num = tag
end

function MJ_Player:get_huitou_fujing_num()
	return self.huitou_fujing_num
end

function MJ_Player:set_jiesan_time()
	self.last_jiesan_time = os.time()
end

function MJ_Player:get_jiesan_time()
	return self.last_jiesan_time
end

function MJ_Player:set_ready(flag)
    self.is_ready_ = flag
end

function MJ_Player:set_is_jiepao(tag)
    self.is_jiepao = tag
end

function MJ_Player:get_is_jiepao()
    return self.is_jiepao
end

function MJ_Player:is_ready()
    return self.is_ready_
end

function MJ_Player:set_stat(i,score) 
	self.score_stat[i] = score
end

function MJ_Player:get_stat() 
    for k,v in pairs(self.score_stat) do
		if v == nil then
            self.score_stat[k] = 0
        end 
    end
	return self.score_stat
end

function MJ_Player:can_out_card( )
	local len_card = #self.hand_ + #self.group_ * 3
	return len_card == 14 and true or false
end

function MJ_Player:out_card_isok( )
	local len_card = #self.hand_ + #self.group_ * 3
	return len_card == 13 and true or false
end


function MJ_Player:add_jingfen(typ, value)
	if typ == 1 then
		self.xia_jingfen = self.xia_jingfen + value
		self.score_ = self.score_ + value
		--self.total_score = self.total_score + value	
	elseif typ == 2 then
		self.huotou_jingfen = self.huotou_jingfen + value
		self.score_ = self.score_ + value
		--self.total_score = self.total_score + value	
	elseif typ == 3 then
		--self.total_score = self.total_score + value	
		self.score_ = self.score_ + value
		self.jingfen = self.jingfen + value
	elseif typ == 4 then -- 明杠
		--self.total_score = self.total_score + value	
		self.score_ = self.score_ + value
		self.minggang = self.minggang + value
	elseif typ == 5 then -- 暗杠
		--self.total_score = self.total_score + value	
		self.score_ = self.score_ + value
		self.angang = self.angang + value
	elseif typ == 99 then
		self.huotou_jingfen = self.huotou_jingfen + value
		self.score_ = self.score_ + value
	end
end

function MJ_Player:decr_jingfen(typ, value)
	if typ == 1 then
		self.xia_jingfen = self.xia_jingfen - value
		self.score_ = self.score_ - value
		--self.total_score = self.total_score - value
	elseif typ == 2 then
		self.huotou_jingfen = self.huotou_jingfen - value
		self.score_ = self.score_ - value
		--self.total_score = self.total_score - value
	elseif typ == 3 then
		--self.total_score = self.total_score - value	
		self.score_ = self.score_ - value
		self.jingfen = self.jingfen - value
	elseif typ == 4 then -- 明杠
		--self.total_score = self.total_score - value	
		self.score_ = self.score_ - value
		self.minggang = self.minggang - value
	elseif typ == 5 then -- 暗杠
		--self.total_score = self.total_score - value	
		self.score_ = self.score_ - value
		self.angang = self.angang - value
	elseif typ == 99 then
		self.huotou_jingfen = self.huotou_jingfen - value
		self.score_ = self.score_ - value
	end
end

function MJ_Player:get_jingfen(typ) 
	if typ == 1 then
		return self.xia_jingfen
	elseif typ == 2 then
		return self.huotou_jingfen
	elseif typ == 3 then
		return self.jingfen
	end
end

-- 设置胡法
function MJ_Player:add_hu_type(hu_type)
    table.insert(self.hu_types,hu_type)
end

function MJ_Player:get_hu_types()
    return self.hu_types
end

function MJ_Player:set_hu_cards(cards)
    self.hu_cards = cards
end

function MJ_Player:get_hu_cards()
    return self.hu_cards
end

function MJ_Player:set_dianpao(flag)
	self.dianpao = flag
end

function MJ_Player:is_dianpao()
	return self.dianpao
end

function MJ_Player:set_zimo(flag)
	self.zimo = flag
end

function MJ_Player:is_zimo()
	return self.zimo
end

function MJ_Player:set_tiandihu(flag)
	self.tiandihu = flag
end

function MJ_Player:is_tiandihu()
	return self.tiandihu
end

function MJ_Player:get_gang_score()
	return self.minggang
end

function MJ_Player:clean_score()
	self.minggang = 0
	self.score_ = 0
end


--增加分数
function MJ_Player:add_score(num)
	self.score_ = self.score_ + num
	--self.total_score = self.total_score + num
end

--增加分数
function MJ_Player:decr_score(num)
	self.score_ = self.score_ - num
	--self.total_score = self.total_score - num
end

function MJ_Player:get_score()
    return self.score_
end

function MJ_Player:add_total_score(tag)
    self.total_score = self.total_score + math.abs(tag)
end

function MJ_Player:decr_total_score(tag)
    self.total_score = self.total_score - math.abs(tag)
end

function MJ_Player:get_total_score()
    return self.total_score
end

function MJ_Player:get_hand()
    return self.hand_
end

function MJ_Player:get_group()
    return self.group_
end

function MJ_Player:set_group(i,cards)
    self.group_[i] = cards
end

function MJ_Player:get_out_cards()
    return self.out_cards_
end

function MJ_Player:get_hand_count()
    return #self.hand_
end

function MJ_Player:has_hand_card( tbl )
    return MJLogic.has_card(self.hand_, tbl)
end

function MJ_Player:insert_hand_card( tbl )
    for k,v in ipairs(tbl) do
        table_insert(self.hand_,v)
        self.last_card = v
    end
end

function MJ_Player:insert_out_card( tbl )
	for k,v in ipairs(tbl) do
		table_insert(self.out_cards_,v)
	end
end

function MJ_Player:remove_out_card()
	table_remove(self.out_cards_)
end

function MJ_Player:delete_hand_card( tbl )
	if #tbl == 1 then
		self.last_card = nil
	end
    MJLogic.delete_card(self.hand_, tbl)
end

function MJ_Player:get_one_min_hand_card()
    local tbl = {}
    local count = self:get_hand_count()
    if count <= 0 then
        return tbl
    end
    table_insert(tbl,self.hand_[count])
    self:delete_hand_card(tbl)
    return tbl
end

function MJ_Player:do_peng(data)
    local has = {data[1],data[1]}
    if self:has_hand_card(has) then
        self:delete_hand_card(has)
        table_insert(self.group_, {data[1],data[1],data[1]})
    end
end

function MJ_Player:do_gang(data,typ,to_uid)
	if typ == 1 then		-- 放杠
		local has = {data[1],data[1],data[1]}
		if self:has_hand_card(has) then
			self:delete_hand_card(has)
			table_insert(self.group_, {data[1],data[1],data[1],data[1],3,to_uid})
			self:add_hu_type(MJLogic.HU_GANG_JIE)
		end
	elseif typ == 2 then	-- 自摸暗杠
		local has = {data[1],data[1],data[1],data[1]}
		if self:has_hand_card(has) then
			self:delete_hand_card(has)
			table_insert(self.group_, {data[1],data[1],data[1],data[1],2})
			self:add_hu_type(MJLogic.HU_GANG_AN)
		end
	elseif typ == 3 then	--  明杠
		for i,group in ipairs(self.group_) do
			if group[1] == data[1] and group[2] == data[1] and group[3] == data[1] then
				local has = {data[1]}
				if self:has_hand_card(has) then
					self:delete_hand_card(has)
					table_insert(group, data[1])
					table_insert(group, 1)
					self:add_hu_type(MJLogic.HU_GANG_GONG)
				end
			end
		end	
	end
	
  --[[  local has = {data[1],data[1],data[1],data[1]}
    if self:has_hand_card(has) then
        self:delete_hand_card(has)
        table_insert(self.group_, {data[1],data[1],data[1],data[1]})
    end--]]
end

function MJ_Player:do_chi_card(out_card, group)
    local cp_group = clone(group)
    MJLogic.delete_card(cp_group, out_card)
    if self:has_hand_card(cp_group) then
        self:delete_hand_card(cp_group)
        table_insert(self.group_, group)
    end
end

return MJ_Player
